﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Core.DataAccess.Model.Project.Entrepot;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IEntrepotModule;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Core.Web.Controllers.Entrepot
{
    /// <summary>
    /// 产品模板
    /// </summary>
    [Route(".v1/Entrepot/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class ProductTemplateController : BaseApiController
    {
        
        public ProductTemplateController(IEntrepot iEntrepot, IEntrepotOperation iEntrepotOperation) :
            base(iEntrepot: iEntrepot, iEntrepotOperation: iEntrepotOperation)
        { }

        /// <summary>
        /// 获取指定产品模板
        /// </summary>
        /// <param name="temp_key">模板关键字</param>
        /// <returns></returns>
        [HttpGet("{temp_key}")]
        [IgnoreAuthorize]
        public IResult GetItemProductTemplate(int temp_key)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GetItemProductTemplate(
                        temp_key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(temp_key > 0, temp_key > 0 ? "" : "params: temp_key is null"));
        }


        
        /// <summary>
        /// 获取卡片信息
        /// </summary>
        /// <returns></returns>
        [HttpGet(".card")]
        [IgnoreAuthorize]
        public IResult GetProductCardContent()
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepotOperation.GetProductCardContent(iProjectInfo.Token));
                });
        }



        /// <summary>
        /// 获取指定分类产品模板
        /// </summary>
        /// <param name="model">查询参数</param>
        /// <returns></returns>
        [HttpPost(".getbytype")]
        [IgnoreAuthorize]
        public IResult GetProductTemplate(EntrepotListRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GetProductTemplate(
                        model.type_key,
                        iProjectInfo.Token,
                        model.pagination), model.pagination);
                },
                new Tuple<bool, string>(model.type_key > 0, model.type_key > 0 ? "" : "params: type_key is null"));
        }


        /// <summary>
        /// 根据标题搜索产品模板
        /// </summary>
        /// <param name="model">查询参数</param>
        /// <returns></returns>
        [HttpPost(".search")]
        [IgnoreAuthorize]
        public IResult GetSeachProductTemplate(EntrepotListRequest model)
        {

            model.LayerTable();
             
            if (string.IsNullOrWhiteSpace(model.search))
            {
                model.search = "";
            }

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.GetSeachProductTemplate(
                        model.search,
                        iProjectInfo.Token,
                        model.pagination), model.pagination);
                });
        }


        /// <summary>
        /// 根据搜索产品模板根据属性
        /// </summary>
        /// <param name="model">查询参数</param>
        /// <returns></returns>
        [HttpPost(".searchByattr")]
        [IgnoreAuthorize]
        public IResult GetSeachProductTemplateByAttributes(EntrepotListRequest model)
        {
            
            model.LayerTable();

            if (string.IsNullOrWhiteSpace(model.search))
            {
                model.search = "";
            }

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepotOperation.GetTemplateByAttributes(
                        iProjectInfo.Token,
                        model.attributes,
                        model.pagination,
                        model.search), model.pagination);
                });
        }



        /// <summary>
        /// 确认产品模板
        /// </summary>
        /// <param name="temp_key">模板关键字</param>
        /// <returns></returns>
        [HttpPost(".confirm{temp_key}")]
        public IResult ConfirmProductTemplate(int temp_key)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ConfirmProductTemplate(
                        temp_key,
                        iProjectInfo.Token));
                },
                new Tuple<bool, string>(temp_key > 0, temp_key > 0 ? "" : "params: temp_key is null"));
        }





        /// <summary>
        /// 添加模板
        /// </summary>
        /// <param name="model">字典实体</param>
        /// <returns></returns>
        [HttpPost]
        public IResult POST(ProductTemplate model)
        {

            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ProductTemplateExec(
                        model,
                        SqlExecType.insert));
                }, model.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 修改模板
        /// </summary>
        /// <param name="model">字典实体</param>
        /// <returns></returns>
        [HttpPut]
        public IResult PUT(ProductTemplate model)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ProductTemplateExec(
                        model,
                        SqlExecType.update));
                }, model.IsValid(IsValidEnum.update));
        }



        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="id">分类关键字</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public IResult Delete(int id)
        {
            return this.ExecMethod(
                () => {
                    return this.Result(this.iEntrepot.ProductTemplateExec(
                        new ProductTemplate
                        {
                            Id = id,
                            ProjectToken = iProjectInfo.Token
                        }, 
                        SqlExecType.delete));
                },
                new Tuple<bool, string>(id > 0, id > 0 ? "" : "params: id is null"));
        }



    }
}